package test0102.librarymanagement;

public class Library {
    private String libraryName;
    private static Book[] books = new Book[100];
    private static int bookIndex = 0;
    public Library() {
    }

    public Library(String libraryId) {
        this.libraryName = libraryId;
    }

    public String getLibraryName() {
        return libraryName;
    }

    public void setLibraryName(String libraryName) {
        this.libraryName = libraryName;
    }

    @Override
    public String toString() {
        return "Library{" +
                "libraryId=" + libraryName +
                '}';
    }

    public void add(Book book) {
        for(int i=0; i<books.length; i++){
            if(books[i]==null){
                books[bookIndex] = book;
                bookIndex++;
                break;
            }
        }
    }

    public void printAll() {
        for(int i=0; i<books.length; i++){
            if(books[i]!=null){
                System.out.println(books[i].toString());
            }
        }
    }

    public Book findBookByAuthor(String name) {
        Book foundBook = null;
        for(int i=0; i<bookIndex; i++){
            if(books[i] != null){
            if(books[i].getAuthor() == name){
                foundBook = books[i];
                break;
            }}
        }
        return foundBook;
    }

    public Book[] findBorrowedBook() {
        Book[] middleBooks = new Book[bookIndex];
        int j = 0;
        for(int i=0; i<bookIndex; i++){
            if(books[i]!=null && books[i].isBorrowed()){
                middleBooks[j] = books[i];
                j++;
            }
        }
        Book[] borrowedBooks = new Book[j];
        for(int i=0; i<j; i++){
            borrowedBooks[i] = middleBooks[i];
        }
        return borrowedBooks;
    }
}
